//
//  OMGNewCollectionViewCell.m
//  A_project2.0
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGNewCollectionViewCell.h"

#import "OMGNewTableViewCell.h"
#import "OMGNewModel.h"

#import <AFNetworking.h>
#import <UIImageView+AFNetworking.h>
#import <MJRefresh.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface OMGNewCollectionViewCell()<UITableViewDataSource,UITableViewDelegate>

{
    NSInteger _page;
}

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end


@implementation OMGNewCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.contentView.frame.size.width, 557) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 滚动条隐藏
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        [self.contentView addSubview:_tableView];
        // 重用池
        [_tableView registerClass:[OMGNewTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        // 数组初始化
        self.dataArr = [NSMutableArray array];
        _page = 1;
        // 刷新加载
        [self refreshUI];
    }
    return self;
}

#pragma mark - 数据处理
- (void)data
{
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%@&forum_type=new&lat=38.89629903285677&lon=121.5291014608641&oauth_token=fd676729155d47e8855e62142686c1be&page=%ld",self.str,_page];
    NSString *url = @"&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=B9CA7896-B35E-4798-A060-4DC4EC2BDCD2&track_os=ios%25208.4.1&track_user_id=6928936&v=1";
    
    [QPNetWorkingTool getNetWorkingTool:[NSString stringWithFormat:@"%@%@",str,url] dic:nil success:^(id result) {
        NSArray *arr = result[@"data"][@"entry"];
        for (NSDictionary *dic in arr) {
            OMGNewModel *m = [OMGNewModel modelWithDic:dic];
            [self.dataArr addObject:m];
            
        }
        
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
#pragma mark - cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    OMGNewModel *m = _dataArr[indexPath.row];
    cell.titleLabel.text = m.username;
    [cell.img setImageWithURL:[NSURL URLWithString:m.avatar]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    cell.xiangqingLabel.text = m.title;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90;
}

#pragma mark - cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    OMGNewModel *m = _dataArr[indexPath.row];
    [self.delegate secWebPush:m.view_url];
    
}

- (void)refreshUI
{
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        //  刷新数据
        [self data];
        
    }];
    self.tableView.mj_header = header;
    [header setTitle:@"loading" forState:MJRefreshStatePulling];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page++;
        // 加载数据
        [self data];
    }];
}

// 重写set方法
-(void)setDataArr:(NSMutableArray *)dataArr{
    
    if (_dataArr != dataArr) {
        _dataArr = dataArr;
    }
    [self.tableView reloadData];
}


@end
